package cn.szzsi.common.model;

import cn.szzsi.common.model.base.BaseSiteConfig;
import com.jfinal.plugin.activerecord.Db;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 实体类 - 管理员
 * 
 */
public class SiteConfig extends BaseSiteConfig<SiteConfig> {

	private static final long serialVersionUID = -57555379613217315L;
	public static final SiteConfig dao = new SiteConfig().dao();

	public static  final SiteConfig getByApiid(String apiid){
		return dao.findFirst("select * from mall_site_config where apiid=? ",apiid);

	}

	public String getShopAliasByShopId(String shopId){
		return Db.queryColumn("select shopAlias from mall_site_config where uid = ?",shopId);
	}

	/**
	 * 根据shopId来获取一个店铺配置
	 * @param shopId
	 * @return
	 */
	public SiteConfig findByShopId(String shopId){
		return dao.findFirstByCache("SiteConfig","sc".concat(shopId),"select * from mall_site_config where uid=?",shopId);
	}

	/**
	 * 根据用户ID查询该用户所拥有的权限列表
	 * @param adminId
	 * @return
	 */
	public List<String> getAuthoritiesName(String adminId) {
		String sql = ""
				+ "select a.value"
				+"  from mall_admin u"
				+"  left outer join mall_admin_role ru"
				+"    on u.id = ru.adminset_id"
				+"  left outer join mall_role r"
				+"    on ru.roleset_id = r.id"
				+"  left outer join mall_role_resource ra"
				+"    on r.id = ra.roleset_id"
				+"  left outer join mall_resource a"
				+"    on ra.resourceset_id = a.id"
				+" where u.id = ?";
		return Db.query(sql, adminId);
	}

	public static SiteConfig getByShopId(String uid){
		return dao.findFirst("select * from mall_site_config where uid=?",uid);
	}

	public SiteConfig getByShopAlias(String shopAlias){
		return dao.findFirst("select * from mall_site_config where shopAlias=?",shopAlias);
	}

	/**
	 * 验证店铺别名是否存在
	 * @param shopAlias
	 * @param uid
     * @return
     */
	public static SiteConfig getByShopAliasAndUid(String shopAlias, String uid){
		return dao.findFirst("select * from mall_site_config where shopAlias=? and uid !=?" ,shopAlias,uid);
	}

	/**
	 * 保存会员店铺信息
	 */
	public static final void saveSiteConfig(int fdjtPercent, int totalIncome1, int totalIncome2, int totalIncome3, int totalIncome4, int totalIncome5, int totalIncome6, int orderPercent1, int orderPercent2, int orderPercent3, int orderPercent4, int orderPercent5, int orderPercent6, int faxian1, int faxian2, int faxian3, int rewardnum1, int rewardnum2, int rewardnum3, int rewardnum4, int rewardnum5, int rewardnum6, int rewardPercent1, int rewardPercent2, int rewardPercent3, int rewardPercent4, int rewardPercent5, int rewardPercent6, String sms_account_sid, String sms_auth_token, String sms_server_ip, String sms_server_port, String sms_appid, String sms_template_id, String uid, String shopName, String shopAlias, String logoUrl, String shopUrl, String hotSearch, String address, String phone, String zipCode, String email, String metaKeywords, String metaDescription, int openWeixnLogin, String appid, int distributionLevels, String rewardPercent, String secret, String weixinIndexAddress, String apiid, int openFenxiao, int openZongjian, String depositName, String shopBackName, int firstSignPoint, int signPoint, int continueSingDays, int extraContinuePoint, int storeWarning, String token,String adminLogo){
		SiteConfig siteConfig=new SiteConfig();
		siteConfig.set("uid",uid);
		siteConfig.set("shopName",shopName);
		siteConfig.set("shopAlias",shopAlias);
		siteConfig.set("shopUrl",shopUrl);
		siteConfig.set("adminLogo",adminLogo);
		siteConfig.set("openWeixnLogin",openWeixnLogin);
		siteConfig.set("weixinIndexAddress",weixinIndexAddress);
		if(StringUtils.isNotBlank(logoUrl)){
			siteConfig.set("logoUrl",logoUrl);
		}
		if(StringUtils.isNotBlank(hotSearch)){
			siteConfig.set("hotSearch",hotSearch);
		}
		if(StringUtils.isNotBlank(address)){
			siteConfig.set("address",address);
		}
		if(StringUtils.isNotBlank(phone)){
			siteConfig.set("phone",phone);
		}
		if(StringUtils.isNotBlank(zipCode)){
			siteConfig.set("zipCode",zipCode);
		}
		if(StringUtils.isNotBlank(email)){
			siteConfig.set("email",email);
		}
		if(StringUtils.isNotBlank(metaKeywords)){
			siteConfig.set("metaKeywords",metaKeywords);
		}
		if(StringUtils.isNotBlank(metaDescription)){
			siteConfig.set("metaDescription",metaDescription);
		}
		if(StringUtils.isNotBlank(appid)){
			siteConfig.set("appid",appid);
		}
		if(StringUtils.isNotBlank(secret)){
			siteConfig.set("secret",secret);
		}
		siteConfig.set("distributionLevels",distributionLevels);
		if(StringUtils.isNotBlank(rewardPercent)){
			siteConfig.set("rewardPercent",rewardPercent);
		}
		siteConfig.set("storeWarning",storeWarning);
		siteConfig.set("apiid",apiid);
		siteConfig.set("token",token);
		siteConfig.set("openFenxiao",openFenxiao);
		siteConfig.set("openZongjian",openZongjian);
		siteConfig.set("depositName",depositName);
		siteConfig.set("shopBackName",shopBackName);
		siteConfig.set("firstSignPoint",firstSignPoint);
		siteConfig.set("signPoint",signPoint);
		siteConfig.set("continueSingDays",continueSingDays);
		siteConfig.set("extraContinuePoint",extraContinuePoint);
		siteConfig.set("sms_account_sid",sms_account_sid);
		siteConfig.set("sms_auth_token",sms_auth_token);
		siteConfig.set("sms_server_ip",sms_server_ip);
		siteConfig.set("sms_server_port",sms_server_port);
		siteConfig.set("sms_appid",sms_appid);
		siteConfig.set("sms_template_id",sms_template_id);
		//分销设置start
		siteConfig.set("faxian1",faxian1);
		siteConfig.set("faxian2",faxian2);
		siteConfig.set("faxian3",faxian3);

		siteConfig.set("rewardnum1",rewardnum1);
		siteConfig.set("rewardnum2",rewardnum2);
		siteConfig.set("rewardnum3",rewardnum3);
		siteConfig.set("rewardnum4",rewardnum4);
		siteConfig.set("rewardnum5",rewardnum5);
		siteConfig.set("rewardnum6",rewardnum6);

		siteConfig.set("rewardPercent1",rewardPercent1);
		siteConfig.set("rewardPercent2",rewardPercent2);
		siteConfig.set("rewardPercent3",rewardPercent3);
		siteConfig.set("rewardPercent4",rewardPercent4);
		siteConfig.set("rewardPercent5",rewardPercent5);
		siteConfig.set("rewardPercent6",rewardPercent6);
		//分销设置end

		//辅导津贴start
		siteConfig.set("fdjtPercent",fdjtPercent);
		siteConfig.set("totalIncome1",totalIncome1);
		siteConfig.set("totalIncome2",totalIncome2);
		siteConfig.set("totalIncome3",totalIncome3);
		siteConfig.set("totalIncome4",totalIncome4);
		siteConfig.set("totalIncome5",totalIncome5);
		siteConfig.set("totalIncome6",totalIncome6);

		siteConfig.set("orderPercent1",orderPercent1);
		siteConfig.set("orderPercent2",orderPercent2);
		siteConfig.set("orderPercent3",orderPercent3);
		siteConfig.set("orderPercent4",orderPercent4);
		siteConfig.set("orderPercent5",orderPercent5);
		siteConfig.set("orderPercent6",orderPercent6);
		//辅导津贴end
		siteConfig.save();
	}
	/**
	 * 修改会员店铺信息
	 */
	public static final void updateSiteConfig(int fdjtPercent, int totalIncome1, int totalIncome2, int totalIncome3, int totalIncome4, int totalIncome5, int totalIncome6, int orderPercent1, int orderPercent2, int orderPercent3, int orderPercent4, int orderPercent5, int orderPercent6, int faxian1, int faxian2, int faxian3, int rewardnum1, int rewardnum2, int rewardnum3, int rewardnum4, int rewardnum5, int rewardnum6, int rewardPercent1, int rewardPercent2, int rewardPercent3, int rewardPercent4, int rewardPercent5, int rewardPercent6, String sms_account_sid, String sms_auth_token, String sms_server_ip, String sms_server_port, String sms_appid, String sms_template_id, SiteConfig siteConfig, String uid, String shopName, String shopAlias, String logoUrl, String shopUrl, String hotSearch, String address, String phone, String zipCode, String email, String metaKeywords, String metaDescription, int openWeixnLogin, String appid, int distributionLevels, String rewardPercent, String secret, String weixinIndexAddress, String apiid, int openFenxiao, int openZongjian, String depositName, String shopBackName, int firstSignPoint, int signPoint, int continueSingDays, int extraContinuePoint, int storeWarning, String token,String adminLogo){
		siteConfig.set("uid",uid);
		siteConfig.set("shopName",shopName);
		siteConfig.set("shopAlias",shopAlias);
		siteConfig.set("shopUrl",shopUrl);
		siteConfig.set("adminLogo",adminLogo);
		siteConfig.set("openWeixnLogin",openWeixnLogin);
		siteConfig.set("weixinIndexAddress",weixinIndexAddress);
		if(StringUtils.isNotBlank(logoUrl)){
			siteConfig.set("logoUrl",logoUrl);
		}
		if(StringUtils.isNotBlank(hotSearch)){
			siteConfig.set("hotSearch",hotSearch);
		}
		if(StringUtils.isNotBlank(address)){
			siteConfig.set("address",address);
		}
		if(StringUtils.isNotBlank(phone)){
			siteConfig.set("phone",phone);
		}
		if(StringUtils.isNotBlank(zipCode)){
			siteConfig.set("zipCode",zipCode);
		}
		if(StringUtils.isNotBlank(email)){
			siteConfig.set("email",email);
		}
		if(StringUtils.isNotBlank(metaKeywords)){
			siteConfig.set("metaKeywords",metaKeywords);
		}
		if(StringUtils.isNotBlank(metaDescription)){
			siteConfig.set("metaDescription",metaDescription);
		}
		if(StringUtils.isNotBlank(appid)){
			siteConfig.set("appid",appid);
		}
		if(StringUtils.isNotBlank(secret)){
			siteConfig.set("secret",secret);
		}
		siteConfig.set("distributionLevels",distributionLevels);
		siteConfig.set("rewardPercent",rewardPercent);
		siteConfig.set("storeWarning",storeWarning);
		siteConfig.set("apiid",apiid);
		siteConfig.set("token",token);
		siteConfig.set("openFenxiao",openFenxiao);
		siteConfig.set("openZongjian",openZongjian);
		siteConfig.set("depositName",depositName);
		siteConfig.set("shopBackName",shopBackName);
		siteConfig.set("firstSignPoint",firstSignPoint);
		siteConfig.set("signPoint",signPoint);
		siteConfig.set("continueSingDays",continueSingDays);
		siteConfig.set("extraContinuePoint",extraContinuePoint);
		siteConfig.set("sms_account_sid",sms_account_sid);
		siteConfig.set("sms_auth_token",sms_auth_token);
		siteConfig.set("sms_server_ip",sms_server_ip);
		siteConfig.set("sms_server_port",sms_server_port);
		siteConfig.set("sms_appid",sms_appid);
		siteConfig.set("sms_template_id",sms_template_id);
		//分销设置start
		siteConfig.set("faxian1",faxian1);
		siteConfig.set("faxian2",faxian2);
		siteConfig.set("faxian3",faxian3);

		siteConfig.set("rewardnum1",rewardnum1);
		siteConfig.set("rewardnum2",rewardnum2);
		siteConfig.set("rewardnum3",rewardnum3);
		siteConfig.set("rewardnum4",rewardnum4);
		siteConfig.set("rewardnum5",rewardnum5);
		siteConfig.set("rewardnum6",rewardnum6);

		siteConfig.set("rewardPercent1",rewardPercent1);
		siteConfig.set("rewardPercent2",rewardPercent2);
		siteConfig.set("rewardPercent3",rewardPercent3);
		siteConfig.set("rewardPercent4",rewardPercent4);
		siteConfig.set("rewardPercent5",rewardPercent5);
		siteConfig.set("rewardPercent6",rewardPercent6);
		//分销设置end

		//辅导津贴start
		siteConfig.set("fdjtPercent",fdjtPercent);
		siteConfig.set("totalIncome1",totalIncome1);
		siteConfig.set("totalIncome2",totalIncome2);
		siteConfig.set("totalIncome3",totalIncome3);
		siteConfig.set("totalIncome4",totalIncome4);
		siteConfig.set("totalIncome5",totalIncome5);
		siteConfig.set("totalIncome6",totalIncome6);

		siteConfig.set("orderPercent1",orderPercent1);
		siteConfig.set("orderPercent2",orderPercent2);
		siteConfig.set("orderPercent3",orderPercent3);
		siteConfig.set("orderPercent4",orderPercent4);
		siteConfig.set("orderPercent5",orderPercent5);
		siteConfig.set("orderPercent6",orderPercent6);
		//辅导津贴end
		siteConfig.update();
	}

	/**
	 * 获取热搜关键词
	 * @param uid
	 * @return
     */
	public List<String> getHotSearchByUid(String uid){
		SiteConfig siteConfig= SiteConfig.dao.findFirst("select * from mall_site_config where uid=?",uid);
		String hotSearch=siteConfig.getStr("hotSearch");
		List<String> list=null;
		if(hotSearch!=null){
			String keywordArray[]=hotSearch.split(",");
			list = Arrays.asList(keywordArray);
		}
		return list;
	}

}

















